package algorithm.model;

/**
 * Node 节点类，用于算法练习使用的结构
 * 增加了toString 以方便打印节点信息
 * @Author Antony
 * @Since 2018/7/6 17:58
 */
public class ListNode {
    public int val;
    public ListNode next;
    public ListNode(int x) {val = x;}

    public String nodeString(){
        StringBuilder bu = new StringBuilder("ListNode:[");
        ListNode curr = this;
        while(curr != null){
            bu.append(curr.val);
            curr = curr.next;
            if(curr != null){
                bu.append(", ");
            }
        }
        bu.append("]");
        return bu.toString();
    }

    // 提供一个初始化节点的方法
    public static ListNode initDate(int start, int end){
        ListNode node = new ListNode(start);
        ListNode head = node;
        for(int i=start+1; i<=end; i++){
            node = node.next = new ListNode(i);
        }
        return head;
    }


    // 指定数据生成链表
    public static ListNode initDate(int[] dates){
        ListNode node = new ListNode(dates[0]);
        ListNode head = node;
        for(int i=1; i<dates.length; i++){
            node = node.next = new ListNode(dates[i]);
        }
        return head;
    }

    @Override
    public String toString() {
        return "ListNode{" +
                "val=" + val +
                ", next=" + next +
                '}';
    }
}
